package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.LogisticHistoryDAO;
import cn.edu.nju.software.greencreditsystem.business.WoleAnalyser;
import cn.edu.nju.software.greencreditsystem.domain.LogisticEnterprise;
import cn.edu.nju.software.greencreditsystem.domain.LogisticHistory;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-2
 * 类描述：LogisticHistory类DAO实现
 */
public class LogisticHistoryDAOImpl implements LogisticHistoryDAO
{

    @Override
    public boolean save(LogisticHistory lh)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.save(lh);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public int count()
    {
        int result = 0;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from LogisticEnterprise");
            result = q.list().size();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean save(LogisticHistory[] lhs)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            for(int i = 0; i < lhs.length; i++)
            {
                s.save(lhs[i]);
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
            return result;
        }finally
        {
            s.close();
        }
        
        s.getTransaction().commit();
        result = true;
        
        return result;
    }

    @Override
    public double[][] getOrderlyGood()
    {
        double result[][];
        List<LogisticHistory[]> temp = new ArrayList();
        List<LogisticEnterprise> enList = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from LogisticEnterprise "
                                    + "where isGood=true ");
            enList = q.list();
            
            for(LogisticEnterprise en:enList)
            {
                Query lhq = s.createQuery("from LogisticHistory where enterprise.id = " + en.getId()
                                + " order by financialFactor");
                List ltemp = lhq.list();
                temp.add(new LogisticHistory[ltemp.size()]);
                
                for(int i = 0; i < ltemp.size(); i++)
                    temp.get(temp.size()-1)[i] = (LogisticHistory) ltemp.get(i);
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        result = new double[temp.size()][temp.get(0).length];
        for(int i = 0; i < temp.size(); i++)
        {
            for(int j = 0; j < temp.get(0).length; j++)
            {
                result[i][j] = temp.get(i)[j].getValue();
            }
        }
        
        return result;
    }

    @Override
    public double[][] getOrderlyBad()
    {
        double result[][];
        List<LogisticHistory[]> temp = new ArrayList();
        List<LogisticEnterprise> enList = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from LogisticEnterprise "
                                    + "where isGood=false ");
            enList = q.list();
            
            for(LogisticEnterprise en:enList)
            {
                Query lhq = s.createQuery("from LogisticHistory where enterprise.id = " + en.getId()
                                + " order by financialFactor");
                List ltemp = lhq.list();
                temp.add(new LogisticHistory[ltemp.size()]);
                
                for(int i = 0; i < ltemp.size(); i++)
                    temp.get(temp.size()-1)[i] = (LogisticHistory) ltemp.get(i);
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        result = new double[temp.size()][temp.get(0).length];
        for(int i = 0; i < temp.size(); i++)
        {
            for(int j = 0; j < temp.get(0).length; j++)
            {
                result[i][j] = temp.get(i)[j].getValue();
            }
        }
        
        return result;
    }

    @Override
    public double[] getEstimateParameter(int enterpriseID, Date date)
    {
        double result[] = new double[WoleAnalyser.factorNames.length];
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("select value from FinancialScore "
                            + "where enterprise.id = " + enterpriseID
                            + " and date = :d "
                            + "order by factor");
            q.setDate("d", date);
            List<Double> temp = q.list();

            for(int i = 0; i < temp.size(); i++)
                result[i] = temp.get(i);
            
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().commit();
        }finally
        {
            s.close();
        }
        
        return result;
    }
}
